import React from 'react'
// ===================================================================== Temp
import SubmitForm from '#cpt/temp/SubmitForm'
// ===================================================================== global declare
const { $fn, $Lazy, $http } = window
// ===================================================================== antd
const Page 	= $Lazy.load(()=>import('#tp/page/page-container'))
const Image = $Lazy.load(()=>import('@antd/image'))
const Box 	= $Lazy.load(()=>import('#tp/box/box'))
//
const $cache= $Lazy.file(()=>import('#private/cache-api'))
const message = $Lazy.file(()=>import('@antd/message'))
// ===================================================================== component
class Index extends React.Component{
	state = {
		data:{},
		customHide: true,	// 控制自定义属性显示或隐藏
		// 提交表单列表
		formData : [
			{ label: '标本条码',		name: 'spec_code', 				full:true, disabled:true,  },
			
			{ label: '送检医院',		name: 'hosp_name', 				full:true, disabled:true,  },
			{ label: '姓名',			name: 'patient_name', 		half:true, required:true,  },
			{ label: '性别',			name: 'sex', 				half:true, required:true, type: 'select', loading:true, idStr:'code',	isKeydown: 'sex' },
			{ label: '年龄一',		name: 'age_first_val', 			half:true },
			{ label: '年龄一类型',	name: 'age_first_type',			half:true, type: 'select', loading:true },
			{ label: '年龄二',		name: 'age_second_val',			half:true },
			{ label: '年龄二类型',	name: 'age_second_type',		half:true, type: 'select', loading:true },
			{ label: '送检科室',		name: 'department_name',  		half:true },
			{ label: '床号',			name: 'bed_no',  			half:true },
			{ label: '门诊/住院号',	name: 'outpatient',  			half:true },
			{ label: '送检医生',		name: 'doctor',   				half:true },
			{ label: '医生电话',		name: 'doctor_phone',  			half:true },
			{ label: '受检人电话',	name: 'patient_phone',  		half:true },
			{ label: '受检人类型',	name: 'case_type', 				half:true, type: 'select', loading:true, idStr:'code',	isKeydown: 'case_type' },
			{ label: '标本性状',		name: 'spec_traits', 			half:true, type: 'select', loading:true, idStr:'code', onChange:({rows})=>{
				if(rows){
					this.submit = { ...this.submit, spec_traits_name: rows.name}
				}
			} },
			{ label: '采集类型',		name: 'collect_type', 			full:true, type: 'radio', mode:'button' },
			{ label: '医院标识',		name: 'hosp_identification',  	full:true },
			{ label: '证件类型', 	name: 'card_type', 				full:true, type:'select', value: '87007', idStr: 'code' },
			{ label: '证件号',		name: 'card_no',  				full:true },
			{ label: '采样日期',		name: 'samp_date',  			half:true, type:'date-time', format:1,	submit:1,	showToday:false },
			{ label: '采样时间',		name: 'samp_time',  			half:true, type:'time' },
			{ label:'病理固定时间',  name:'bingli_coll_time',  full:true,   type:'date-time',  format:2 },
			{ label:'病理离体时间',	name:'bingli_take_time',	full:true, 	type:'date-time',	format:2 },
			{ label: '识别标签',		name: 'tags', 					full:true, type: 'checkboxGroup', idStr:'code' },
			{ label: '临床诊断',		name: 'diagnosis_info', 		full:true, type: 'textarea' },
			{ label: '备注',		name: 'remark', 				full:true, type: 'textarea' },
			// 病人基本信息 增加5个自定义字段 2021-10-15  --马婷
			{ hide:true,	custom:true, onClick:()=>{this.customClick()}},
			{ label:'备用1', 		name:'user_defined1', 				full:true,	hide:true },
			{ label:'备用2', 		name:'user_defined2', 				full:true,	hide:true },
			{ label:'备用3', 		name:'user_defined3', 				full:true,	hide:true },
			{ label:'备用4', 		name:'user_defined4', 				full:true, 	hide:true },
			{ label:'备用5', 		name:'user_defined5', 				full:true, 	hide:true },
			{ label:'备用6', 		name:'user_defined6', 				full:true, 	hide:true },
			{ label:'备用7', 		name:'user_defined7', 				full:true, 	hide:true },
			{ label:'备用8', 		name:'user_defined8', 				full:true, 	hide:true },
			{ label:'备用9', 		name:'user_defined9', 				full:true, 	hide:true },
			{ label:'备用10', 		name:'user_defined10', 				full:true, 	hide:true },
		],
	}
	sumbit = {}
	
	componentDidMount(){
		const { spec_code } = $fn.query()
		if(spec_code){
			this.spec_code = spec_code
		}
	}
	
	// 点击展开或关闭自定义属性
	customClick = () => {
		const { customHide, formData } = this.state
		this.setState({customHide:!customHide},()=>{
			$fn.hideRows(formData,['user_defined1','user_defined2','user_defined3','user_defined4','user_defined5','user_defined6','user_defined7','user_defined8','user_defined9','user_defined10'], this.state.customHide)
			this.setState({formData})
		})
	}
	
	load = async (spec_code, page_source_modal) => {
		await this.getSelect()
		
		$http.submit(null, 'spec-case-info/info', { param:{ spec_code }, isPull:true }).then(data=>{
			const { formData } = this.state
			const { page_source } = $fn.query()
			
			this.initSubmit = {...data, entry_type:'test', page_source: page_source || page_source_modal}
			// 采样日期不能大于接收日期
			const sampDate = $fn.getRow( formData, 'samp_date')
			const { check_time } = data
			
			if( check_time ){
				const check = check_time.split(' ')
				sampDate.defaultPickerValue = check[0]
				sampDate.after = check[0]
			}
			
			this.setState({ data, formData })
			
			setTimeout(()=> this.formRef.setValue(data))
		})
	}
	
	// 获取下拉数据
	getSelect = async () => {
		const { formData } = this.state
		$fn.setModalRows(formData, ['sex',  'age_first_type','collect_type'], ()=>{
			$cache.then( async f => {
				// 字典
				const dic = await f.getDicCache({ code:[45700, 21000, 42000, 69700, 87000 ] })
				// 性别
				$fn.setRow( formData, 'sex', dic['45700'] )
				// 受检人类型
				$fn.setRow( formData, 'case_type', dic['42000'] )
				// 标本性状
				$fn.setRow( formData, 'spec_traits', dic['21000'] )
				//识别标签
				$fn.setRow( formData, 'tags', dic['69700'] )
				// 年龄类型
				const age = $fn.setRow( formData, 'age_first_type',  await f.getDisCache({ code: 30000 }) )
				$fn.setRow( formData, 'age_second_type', age )
				// 采集类型
				$fn.setRow( formData, 'collect_type', f.collect_type )
				// 身份证类型 [2021-8-17 3:19:24 ?F10: PM?] --伍智睿
				$fn.setRow( formData, 'card_type', dic['87000'])
				
				//
				this.setState({ formData })
			})
		})
	}
	
	render(){
		const { data, customHide } = this.state
		const { onRefresh, pop } = this.props
		return (
			<Page nobc>
				<section className='wh fx'>
					<Box title='病人详细信息' flex className='ex mr5 fv'  contentClassName='fv'>
						{
							SubmitForm.call(this,{ 
								contentClassName	: 'pr10',
								okText				: '提交审核',
								labelWidth			: 's7', 
								btnSize				: 'large',
								scroll				: true,
								hideButton			: pop,
								api					:'spec-case-info/pass',
								onRefresh,
								backRefresh : !pop,
								customHide,
								onForm 				: () => { if(this.spec_code){ this.load(this.spec_code) } },
								onIf 				: ({ samp_date, samp_time }) => {
									let { check_time } = data
									let samp = (samp_date || '') + ' ' + (samp_time || '')
									
									if(samp){
										check_time = new Date(check_time).getTime()
										samp = new Date(samp).getTime()
										
										// 采样日期不能大于接收日期
										if(check_time < samp){
											message.then(f => f.warning(<>采样日期不能大于接收日期<span className='ml5' style={{color:'red'}}>{data.check_time}</span></>))
											return false
										}else{
											return true
										}
									}else{
										return true
									}
								}
							}) 
						}
					</Box>
					<Box title='申请单图片' flex className='ex' contentClassName='fv'>
						<Image className='ex mt10' src={ data.img_url }/>
					</Box>
				</section>
			</Page>
			
		)
	}
}

export default Index